<!-- 分配权限的弹层 -->
<template>
  <div>
    <!-- 权限点数据展示 -->
      <el-tree
        ref='refTree'
        :data="list"
        :props="{ label: 'name' }"
        default-expand-all
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
      />
      <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small">取消</el-button>
        <el-button size="small" type="primary" @click="hSave">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { ListToTree } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    roleId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      list: [],
  }
},
 created() {
// 调用接口，获取所有的权限点数据
   this.loadPermissionList()
// // 调用接口，获取当前这个角色已经具备的权限
//    this.loadPermissionByRoleId()
  },
  methods: {
    async hSave() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      const res = await assignPerm({id: this.roleId, permIds: permIds})
      this.$message.success('保存角色的权限成功')
      this.$emit('success')
    },
    async loadPermissionList() {
      const res = await getPermissionList()
      this.list = ListToTree(res.data)
    },
      // 获取角色现有的权限
    async loadPermissionByRoleId() {
      const res = await getRoleDetail(this.roleId)
      this.$refs.refTree.setCheckedKeys(res.data.permIds)  
    }
  }
}
</script>
 
<style>

</style>